package com.tianyu.inspection.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tianyu.inspection.common.Constants;
import com.tianyu.inspection.common.enums.RoState;
import com.tianyu.inspection.common.annotation.Permission;
import com.tianyu.inspection.common.component.Validate;
import com.tianyu.inspection.entity.OrthographicImage;
import com.tianyu.inspection.entity.model.OperateMo;
import com.tianyu.inspection.entity.model.QueryMo;
import com.tianyu.inspection.entity.vo.Ro;
import com.tianyu.inspection.entity.vo.ServiceRo;
import com.tianyu.inspection.service.api.IOrthographicImageService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Objects;

/**********************************
 * @Author YSW
 * @Description
 * @Date 2021/1/15 - 18:23
 **********************************/

@RestController
@RequestMapping("/api/2.0/orthographicImage")
public class OrthographicImageController {

    @Resource
    private IOrthographicImageService orthographicImageService;

    @GetMapping(value = "/list")
    @Permission(value = 3, tags = Constants.API_COMPANY)
    public Ro<Object> list(HttpServletRequest request, @RequestHeader("u-projectId") Integer projectId) {
        Ro<Object> ro = new Ro<>();
        if (projectId == 1){
            QueryMo queryMo = QueryMo.getQueryMo(request);
            queryMo.build(OrthographicImage.class, projectId);
            ro.setContent(orthographicImageService.searchListPage(queryMo));
            return ro;
        }
        QueryMo queryMo = new QueryMo();
        queryMo.build(OrthographicImage.class, projectId);
        queryMo.put("pid", -1);
        IPage<OrthographicImage> page = orthographicImageService.searchListPage(queryMo);
        List<OrthographicImage> records = page.getRecords();
        queryMo.remove("pid");
        records.forEach(orthographicImage -> {
            orthographicImage.setPid(null);
            orthographicImage.setPipelineId(null);
            queryMo.getEqMap().put("pid", orthographicImage.getId());
            List<OrthographicImage> list = orthographicImageService.searchListPage(queryMo).getRecords();
            list.forEach(orthographicImage1 -> {
                orthographicImage1.setPid(null);
                orthographicImage1.setProjectId(null);
                orthographicImage.setProjectId(null);
            });
            orthographicImage.setList(list);
        });
        ro.setContent(records);
        return ro;
    }

    @GetMapping(value = "/group/list")
    @Permission(value = 1, tags = Constants.API_COMPANY)
    public Ro<Object> groupList( @RequestHeader("u-projectId") Integer projectId) {
        Ro<Object> ro = new Ro<>();
        QueryMo queryMo = new QueryMo();
        queryMo.build(OrthographicImage.class, projectId);
        queryMo.put("pid", -1);
        queryMo.setPageSize(Integer.MAX_VALUE);
        IPage<OrthographicImage> page = orthographicImageService.searchListPage(queryMo);
        List<OrthographicImage> records = page.getRecords();
        ro.setContent(records);
        return ro;
    }

    @GetMapping(value = "/{id}")
    @Permission(value = 3, tags = Constants.API_COMPANY)
    public Ro<OrthographicImage> getOne(@PathVariable Integer id, @RequestHeader("u-projectId") Integer projectId) {
        Ro<OrthographicImage> ro = new Ro<>();
        if (Objects.isNull(id)) {
            return ro.setResultEnum(RoState.PARAMS_MISS_ERROR);
        }
        QueryMo queryMo = new QueryMo();
        queryMo.put("id", id);
        queryMo.build(OrthographicImage.class, projectId);
        OrthographicImage orthographicImage = orthographicImageService.selectOne(queryMo);
        if (Objects.isNull(orthographicImage)) {
            ro.setMessage("未查询到数据");
        } else {
            ro.setContent(orthographicImage);
        }
        return ro;
    }

    @PostMapping(value = "/admin/insert")
    @Permission(value = 1, tags = Constants.API_SUPER)
    public Ro<OrthographicImage> insert(@RequestBody OperateMo operateMo, @RequestHeader("u-projectId") Integer projectId) {
        Ro<OrthographicImage> ro = new Ro<>();
        Integer projectId1 = (Integer) operateMo.get("projectId");
        //是否是分组
        Integer isGroup = (Integer) operateMo.get("isGroup");
        //正射分组
        if (isGroup == 1){
            if (Objects.isNull(projectId1)) {
                return ro.setResultEnum(RoState.FAIL).setMessage("缺少参数[projectId]");
            }else {
                if (validateModel.projectIdValidate(projectId1)) {
                    return ro.setResultEnum(RoState.FAIL).setMessage("参数错误[projectId],值不存在");
                }
            }
        }
        //正射
        if (isGroup == 0){
            if (Objects.nonNull(projectId1)) {
                operateMo.getEntity().remove("projectId");
            }
        }
        operateMo.createBuild(OrthographicImage.class, projectId1);
        //正射
        if (isGroup == 0){
            if (Objects.nonNull(operateMo.get("pid"))) {
                QueryMo queryMo = new QueryMo();
                queryMo.put("id", operateMo.get("pid"));
                queryMo.build(OrthographicImage.class, projectId1);
                OrthographicImage orthographicImage = orthographicImageService.selectOne(queryMo);
                if (Objects.nonNull(orthographicImage)) {
                    if (!Objects.isNull(orthographicImage.getPid())) {
                        return ro.setResultEnum(RoState.FAIL).setMessage("参数错误[pid],值存在父元素,不能当做其他元素父元素");
                    }
                } else {
                    return ro.setResultEnum(RoState.FAIL).setMessage("参数错误[pid],值不存在");
                }
            } else {
                return ro.setResultEnum(RoState.FAIL).setMessage("缺少参数[pid]");
            }
        //正射分组
        }else if (isGroup == 1){
            operateMo.getEntity().remove("url");
        }else {
            return ro.setResultEnum(RoState.FAIL).setMessage("参数错误[isGroup]");
        }
        if (!orthographicImageService.insert(operateMo)) {
            ro.setResultEnum(RoState.FAIL);
        }
        return ro;
    }

    @Resource
    private Validate validateModel;

    @PatchMapping(value = "/admin/modify")
    @Permission(value = 1, tags = Constants.API_SUPER)
    public Ro<OrthographicImage> modify(@RequestBody OperateMo operateMo, @RequestHeader("u-projectId") Integer projectId) {
        Ro<OrthographicImage> ro = new Ro<>();
        final ServiceRo serviceRo = operateMo.updateBuild(OrthographicImage.class, projectId);
        if (!serviceRo.getResult()){
            return ro.setResultEnum(RoState.FAIL).setMessage(serviceRo.getMessage());
        }
        if (operateMo.getEntity().size() == 0) {
            return ro.setResultEnum(RoState.FAIL).setMessage("无操作参数传入,请传参后重试");
        }
        if (Objects.isNull(operateMo.get("id"))) {
            return ro.setResultEnum(RoState.FAIL).setMessage("缺少[id]字段,请传参后重试");
        }
        operateMo.getEqMap().remove("project_id");
        operateMo.getEntity().put("projectId",operateMo.get("projectId"));
        if (!orthographicImageService.update(operateMo)) {
            ro.setResultEnum(RoState.FAIL).setMessage("参数无匹配,修改失败");
        }
        return ro;
    }

    @DeleteMapping(value = "/admin/delete/{id}")
    @Permission(value = 1, tags = Constants.API_SUPER)
    public Ro<OrthographicImage> delete(@PathVariable Integer id, @RequestHeader("u-projectId") Integer projectId) {
        Ro<OrthographicImage> ro = new Ro<>();
        QueryMo queryMo = new QueryMo();
        queryMo.put("id", id);
        queryMo.build(OrthographicImage.class, projectId);
        if (!orthographicImageService.delete(queryMo)) {
            ro.setResultEnum(RoState.FAIL).setMessage("无匹配ID,删除失败");
        }
        return ro;
    }

    @DeleteMapping(value = "/admin/delete/logical/{id}")
    @Permission(value = 1, tags = Constants.API_SUPER)
    public Ro<OrthographicImage> logicallyDelete(@PathVariable Integer id, @RequestHeader("u-projectId") Integer projectId) {
        Ro<OrthographicImage> ro = new Ro<>();
        QueryMo queryMo = new QueryMo();
        queryMo.put("id", id);
        queryMo.build(OrthographicImage.class, projectId);
        if (!orthographicImageService.logicallyDelete(queryMo)) {
            ro.setResultEnum(RoState.FAIL).setMessage("无匹配ID,删除失败");
        }
        return ro;
    }
}
